package game.module.mail.logic;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import game.module.mail.MailConstants;
import game.module.mail.bean.MailList.MailAttachment;
import game.module.mail.bean.MailList.MailItem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MailAction {

	@Autowired
	private MailManager mailManager;

	/**
	 * 发送纯文本邮件
	 * 
	 * @param title
	 * @param content
	 * @param fromRoleId
	 * @param toRoleId
	 */
	public void sendPlainMail(String title, String content, int fromRoleId, int toRoleId) {
		MailItem mailItem = new MailItem();
		mailItem.setTitle(title);
		mailItem.setContent(content);
		mailItem.setSenderId(fromRoleId);
		mailItem.setReceiverId(toRoleId);
		mailItem.setSendTime(new Date().getTime());
		mailItem.setIsRead(MailConstants.MAIL_TYPYE_NEW);
		mailManager.asyncSendMail(mailItem);
	}

	/**
	 * 发送带附件的邮件
	 * 
	 * @param title
	 * @param content
	 * @param fromRoleId
	 * @param toRoleId
	 * @param tplIdList
	 * @param tplCountList
	 */
	public void sendTemplateAttachMail(String title, String content, int fromRoleId, int toRoleId, int[] tplIdList, int[] tplCountList) {
		MailItem mailItem = new MailItem();
		mailItem.setTitle(title);
		mailItem.setContent(content);
		mailItem.setSenderId(fromRoleId);
		mailItem.setReceiverId(toRoleId);
		mailItem.setSendTime(new Date().getTime());
		mailItem.setIsRead(MailConstants.MAIL_TYPYE_NEW);
		List<MailAttachment> attachList = new ArrayList<MailAttachment>();
		mailItem.setMailAttachmentList(attachList);
		for (int i = 0; i < tplIdList.length; i++) {
			MailAttachment mailAttachment = new MailAttachment(tplIdList[i], tplCountList[i]);
			attachList.add(mailAttachment);
		}
		mailManager.asyncSendMail(mailItem);
	}

}
